import {Component, OnInit, ViewChild} from '@angular/core';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {FileManagementService} from '../../file-management/file-management.service';
import {UploadFileComponent} from '../../../common/upload-file/upload-file.component';


@Component({
  selector: 'app-file-management',
  templateUrl: './share-file-management.component.html',
  styleUrls: ['./share-file-management.component.scss']
})
export class ShareFileManagementComponent implements OnInit {
  @ViewChild(UploadFileComponent, {static: true})
  public uploadFileComponent: UploadFileComponent;
  public tableModel: Array<{
    validTime: number;
    id: string;
    name: string;
    size: number;
    createTime: string;
    isEffective: string
  }>;
  public checkedModel: any;
  public isAllChecked: boolean;
  public currentObj = {drawCode: '', shareUrl: ''};
  public isChecked: boolean;
  public isUrlVisible: boolean;
  public shareUrl: string;

  constructor(public fileManagementService: FileManagementService,
              private modalService: NzModalService,
              private message: NzMessageService) {
    this.isUrlVisible = false;
    this.tableModel = [];
    this.checkedModel = {};
    this.isChecked = true;
  }

  // 初始化
  ngOnInit(): void {
    this.getData();
  }

  // 获取数据
  public getData() {
    this.checkedModel = [];
    this.isAllChecked = false;
    this.tableModel = [];
    this.fileManagementService.getShareFileList().subscribe(res => {
      this.tableModel = res;
      for (const table of this.tableModel) {
        if (table.validTime === 0) {
          table.isEffective = '有效';
        } else {
          if ((new Date(table.createTime.replace(/-/g, '/')).getTime() + table.validTime) >= new Date().getTime()) {
            table.isEffective = '有效';
          } else {
            table.isEffective = '已失效';
          }
        }
      }
    });
  }


  // 选择
  public checkboxChoice(data) {
    // 全部选择
    this.isAllChecked = this.tableModel.every((item) => {
      return this.checkedModel[item.id];
    });
    // 设置选中后可以操作的按钮
    this.isChecked = true;
    this.tableModel.forEach(item => {
      if (this.checkedModel[item.id]) {
        this.isChecked = false;
      }
    });
    // 选中，取消
    if (this.checkedModel[data.id]) {
      this.checkedModel[data.id] = data;
    } else {
      delete this.checkedModel[data.id];
    }
  }

  // 全选
  public checkAll() {
    if (this.isAllChecked) {
      for (const folder of this.tableModel) {
        this.checkedModel[folder.id] = folder;
      }
      this.isChecked = false;
    } else {
      for (const folder of this.tableModel) {
        delete this.checkedModel[folder.id];
      }
      this.isChecked = true;
    }
  }

  // 排序
  public sort(event, ...events): void {
    for (const ev of events) {
      if (ev.nzSortKey !== event.key) {
        ev.nzSort = null;
      }
    }
    const array = this.tableModel;
    if (event.value === 'ascend') {
      if (event.key === 'name') {
        array.sort((a, b) => {
          return a[event.key].localeCompare(b[event.key], 'zh-CN');
        });
      } else if (event.key === 'createTime') {
        array.sort((a, b) => {
          return new Date(a[event.key].replace(/-/g, '/')).getTime() - new Date(b[event.key].replace(/-/g, '/')).getTime();
        });
      } else if (event.key === 'size') {
        array.sort((a, b) => {
          return parseFloat(a[event.key]) - parseFloat(b[event.key]);
        });
      } else if (event.key === 'downloadNum') {
        array.sort((a, b) => {
          return parseFloat(a[event.key]) - parseFloat(b[event.key]);
        });
      } else if (event.key === 'browseNum') {
        array.sort((a, b) => {
          return parseFloat(a[event.key]) - parseFloat(b[event.key]);
        });
      } else if (event.key === 'preserveNum') {
        array.sort((a, b) => {
          return parseFloat(a[event.key]) - parseFloat(b[event.key]);
        });
      }
    } else if (event.value === 'descend') {
      array.reverse();
    } else {
      this.getData();
    }
    this.tableModel = JSON.parse(JSON.stringify(array));
  }


  // 删除
  public delete(data, bool) {
    this.modalService.confirm({
      nzTitle: '确认要取消共享吗?',
      nzContent: '<b style="color: red;">取消共享后当前地址将无法访问！</b>',
      nzOkText: '确认',
      nzOkType: 'danger',
      nzOnOk: () => {
        const fid = [];
        if (bool) {
          fid.push(data);
        } else {
          for (const key in data) {
            fid.push(key);
          }
        }
        this.fileManagementService.deleteShareFile(fid).subscribe(res => {
          this.modalService.success({
            nzTitle: '取消共享',
            nzContent: '取消共享成功！',
            nzOnOk: () => {
            }
          });
          this.checkedModel = {};
          this.isAllChecked = false;
          this.isChecked = true;
          this.getData();
        });
      },
      nzCancelText: '取消'
    });
  }

  public lookOver(data) {
    this.isUrlVisible = true;
    this.currentObj = data;
    this.currentObj.shareUrl = location.origin + '/#/share/' + data.code;
  }

  /**
   * 复制
   */
  public copy(data) {
    const input = document.createElement('input');
    input.value = data;
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
      this.message.success('复制成功！');
    } else {
      this.message.success('复制失败！');
    }
  }
}
